//
//  YMHomeReusableView.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/12.
//  Copyright © 2018年 youmy. All rights reserved.
//  首页头部

import UIKit

private let kCycleCellID = "kCycleCellID"

class YMHomeReusableView: UICollectionReusableView {
    @IBOutlet weak var bannerView: UIView!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var runOnV: UIView!
    
    lazy var cycleView = YMHomeCycleView.nibView()
    lazy var runOnView = YMHomeRunOnView.nibView()
    
    var recommansModels : [[String:String]]? {
        didSet {
            // 1.刷新collectionView
            collectionView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        cycleView.cycleModels = ["https://gzhlw-files.oss-cn-shanghai.aliyuncs.com/image/oss/banner/460970116860206.png",
                                 "https://gzhlw-files.oss-cn-shanghai.aliyuncs.com/image/oss/banner//4947296697399313_2f54dc784d4a467fb41be973d26176ea.jpg",
                                 "https://gzhlw-files.oss-cn-shanghai.aliyuncs.com/image/oss/banner//5360038724966435_950da3fa593f4112a81b2959e4e77599.jpg"]
        bannerView.addSubview(cycleView)
        runOnV.addSubview(runOnView)
        
        autoresizingMask = UIViewAutoresizing()
        collectionView.register(UINib.init(nibName: "YMRecommandCell", bundle: nil), forCellWithReuseIdentifier: kCycleCellID)
        collectionView.dataSource = self
        collectionView.delegate = self
        cycleView.didSelectBlock = {(view, index) in
            print("点击了顶部banner：\(index)")
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let kItemW = collectionView.bounds.width / 3
        let kItemH = collectionView.bounds.height
        
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: kItemW, height: kItemH)
        
    }
}

// MARK:- 提供一个快速创建View的类方法
extension YMHomeReusableView{
    class func nibView() -> YMHomeReusableView {
        return Bundle.main.loadNibNamed("YMHomeReusableView", owner: nil, options: nil)?.first as! YMHomeReusableView
    }
}

// MARK:- 遵守UICollectionView的数据源协议
extension YMHomeReusableView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return recommansModels?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCycleCellID, for: indexPath) as! YMRecommandCell
        let dic = recommansModels![indexPath.item]
        let image = UIImage(named: dic["icon"] ?? "")
        cell.iconIV.image = image
        cell.titleLbl.text = dic["title"] ?? ""
        return cell
    }
}

// MARK:- 遵守UICollectionViewDelegate的数据源协议
extension YMHomeReusableView : UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dic = recommansModels![indexPath.item]
        print("点击了：\(dic["title"] ?? "")")
    }
}

